#include "stdafx.h"
#include "wf_drawcircle.h"

#include "pf_snapper.h"



WF_DrawCircle::WF_DrawCircle(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw circles",
                           container, graphicView) {
    reset();
}



WF_DrawCircle::~WF_DrawCircle() {}


QAction* WF_DrawCircle::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
/*     QAction* action = new QAction(tr("Circle: Center, Point"),
                                  tr("Center, &Point"),
                                  QKeySequence(), NULL); */
	// "Circle: Center, Point"
    QAction* action = new QAction(tr("Center, &Point"), NULL);
	action->setIcon(QIcon(":/extui/circles.png"));
    //action->zetStatusTip(tr("Draw circles with center and point"));
    return action;
}

void WF_DrawCircle::reset() {
    data = PF_CircleAttribute(PF_Vector(false), 0.0);
}



void WF_DrawCircle::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();
}



void WF_DrawCircle::trigger() {
    PF_PreviewActionInterface::trigger();

    PF_Circle* circle = new PF_Circle(container,
                                      data);
    circle->setLayerToActive();
    circle->setPenToActive();
    container->addEntity(circle);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(circle);
        document->endUndoCycle();
    }
	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(circle->getCenter());

    setStatus(SetCenter);
    reset();

    PF_DEBUG->print("PF_ActionDrawCircle::trigger(): circle added: %d",
                    circle->getId());
}



void WF_DrawCircle::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawCircle::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);
    switch (getStatus()) {
    case SetCenter:
        data.center = mouse;
        break;

    case SetRadius:
        if (data.center.valid) {
            data.radius = data.center.distanceTo(mouse);
            deletePreview();
            preview->addEntity(new PF_Circle(preview,
                                             data));
            drawPreview();
        }
        break;
    }

    PF_DEBUG->print("PF_ActionDrawCircle::mouseMoveEvent end");
}



void WF_DrawCircle::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawCircle::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetCenter:
        data.center = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetRadius);
        break;

    case SetRadius:
        if (data.center.valid) {
            graphicView->moveRelativeZero(mouse);
            data.radius = data.center.distanceTo(mouse);
            trigger();
        }
        //setStatus(SetCenter);
        break;

    default:
        break;
    }
}



void WF_DrawCircle::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    switch (getStatus()) {

    case SetRadius: {
            bool ok;
            double r = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.radius = r;
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(
                        tr("Not a valid expression"));
                }
            }
            trigger();
            //setStatus(SetCenter);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_DrawCircle::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}


void WF_DrawCircle::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetCenter:
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify center"), tr("Cancel"));
        }
        break;
    case SetRadius:
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify radius"), tr("Back"));
        }
        break;
    default:
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->updateMouseWidget("", "");
        }
        break;
    }
}



void WF_DrawCircle::showOptions() {
    PF_ActionInterface::showOptions();
}



void WF_DrawCircle::hideOptions() {
    PF_ActionInterface::hideOptions();
}



void WF_DrawCircle::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawCircle::updateToolBar() {
    if (!isFinished()) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        }
    } else {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarCircles);
        }
    }
}


// EOF

